﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Rainy.FrameWork
{
    /// <summary>
    /// 公共方法置类
    /// </summary>
    public class Common
    {
        private Common()
        {
        }
        /// <summary>
        /// 得到Request值
        /// </summary>
        /// <param name="request"></param>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public static string GetRequestString(System.Web.HttpRequest request, string paramName)
        {
            return GetRequestString(request, paramName, "");
        }
        /// <summary>
        /// 得到Request值
        /// </summary>
        /// <param name="request"></param>
        /// <param name="paramName"></param>
        /// <param name="defaultString"></param>
        /// <returns></returns>
        public static string GetRequestString(System.Web.HttpRequest request, string paramName, string defaultString)
        {
            string value = string.Empty;
            try
            {
                value = ((request.QueryString[paramName] == null || request.QueryString[paramName].ToString() == "") ? defaultString : request.QueryString[paramName].ToString());
            }
            catch
            {
            }
            return value;
        }
        /// <summary>
        /// 清除可能的SQL注入
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static string ClearInput(string inputString)
        {
            Regex keywordRegex = new Regex(@"\bexec\b|\binsert\b|\bdelete\b|\bupdate\b|\bdrop\b|\btruncate\b|\bselect\b|\bfrom\b|=|;|'|<>|!=|\balter\b|\bwhere\b|\bjoin\b", RegexOptions.IgnoreCase);
            return keywordRegex.Replace(inputString, "");
        }

        /// <summary>
        /// 获取一定长度的字符串加...
        /// </summary>
        /// <param name="text"></param>
        /// <param name="maxLength"></param>
        public static string GetMaxTextString(object text, int maxLength)
        {
            if (text.ToString().Length > maxLength)
            {
                return text.ToString().Substring(0, maxLength) + "...";
            }
            else
            {
                return text.ToString();
            }
        }
    }
}
